<?php
if($_SESSION['jns_barang']!=1){
	#echo "Akses tidak diizinkan";
	#exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
<style type="text/css">
#content div.box table td#biru{background-color: #498cf7}
#content div.box table td#kuning{background-color: #ffd76e}
#content div.box table td#merah{background-color: #d84800}
</style>
					<!-- box / title -->
					<div class="title">
						<h5>Data Jenis Barang Produksi</h5>								
						<div class="search global-nav">
							<form method="GET" class="form-search js-search-form" action="index.php?pages=jenis_barang" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="jenis_barang">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="nama" <?php echo ($field=="nama_brg")?"selected":"";?>>Jenis Barang</option>
									<option value="kode" <?php echo ($field=="kode_brg")?"selected":"";?>>Kode Barang</option>															
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th>Kode Barang</th>
									<th>Jenis Barang Produksi</th>									
									<th class="action"><a href="?pages=jenis_barang&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php										
							
							$addQuery='';

							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlJnsBarang = mysql_query("SELECT * FROM tb_jns_barang $addQuery ORDER BY id_jns_brg DESC limit $posisi, $batas");
							
							$no=$posisi+1;
							while($dataJnsBarang = mysql_fetch_array($sqlJnsBarang)){																			
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td ><?php echo $dataJnsBarang['kode_brg'];?></td>
									<td ><?php echo $dataJnsBarang['nama_brg'];?></td>									
									<td  class="action"><a href="?pages=jenis_barang&act=edit&id=<?php echo $dataJnsBarang['id_jns_brg'];?>" class="icon-edit" title="Ubah Data"></a></td>																	
								</tr>
							<?php	
								$no++;				
							}

							$sqlJnsBarang2 = mysql_query("SELECT * FROM tb_jns_barang $addQuery ORDER BY id_jns_brg DESC");
							$jumlahData = mysql_num_rows($sqlJnsBarang2);

							$file="?pages=jenis_barang&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_jns_brg = (int) filter($_GET['id']);

	$sqlJnsBarang = mysql_query("SELECT * FROM tb_jns_barang WHERE id_jns_brg=$id_jns_brg");
	$dataJnsBarang = mysql_fetch_array($sqlJnsBarang);	

	if(!empty($id_jns_brg)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Jenis Barang Produksi</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Data Jenis Barang Produksi</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<form method="POST" action="?pages=jenis_barang&act=update">
		<input type="hidden" name="id_jns_brg" value="<?php echo $dataJnsBarang['id_jns_brg'];?>">
		<input type="hidden" name="kode_brg" value="<?php echo $dataJnsBarang['kode'];?>">
		<table style="width:auto">
		   	<tbody>		   		
			   	<tr>
					<td><label>Kode Barang</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataJnsBarang['kode_brg'];?>" size="60" name="kode_brg" id="kode_brg" placeholder="Kode Barang" required></td>
			   	</tr>
			   	<tr>
					<td><label>Nama Jenis Barang Produksi</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataJnsBarang['nama_brg'];?>" size="60" name="nama_brg" id="nama_brg" placeholder="Nama Jenis Barang Produksi" required></td>
			   	</tr>			   	
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">			 		</td>
		   		</tr>
		 	</tbody>
		</table>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_jns_brg = (int) $_POST['id_jns_brg'];
	$nama_brg = $_POST['nama_brg'];
	$kode_brg = $_POST['kode_brg'];	
	

	if(!empty($id_jns_brg)){
		$update = mysql_query("UPDATE tb_jns_barang SET nama_brg='$nama_brg', kode_brg='$kode_brg' WHERE id_jns_brg=$id_jns_brg");		

		if($update)
			sukses('Data berhasil disimpan', '?pages=jenis_barang');
		else
			sukses('Data gagal disimpan', '?pages=jenis_barang');
			
	}else{
		$cek = mysql_num_rows(mysql_query("SELECT kode_brg FROM tb_jns_barang WHERE kode_brg='$kode_brg'"));
		if($cek>0){
			sukses('Kode Jenis Barang Produksi Sudah Ada', '?pages=jenis_barang');
			exit;
		}

		$insert = mysql_query("INSERT INTO tb_jns_barang(id_jns_brg, kode_brg, nama_brg) VALUES(NULL, '$kode_brg', '$nama_brg')");	

		$currentJns = mysql_fetch_array(mysql_query("SELECT id_jns_brg FROM tb_jns_barang ORDER BY id_jns_brg DESC LIMIT 1"));

		$insertTemp = mysql_query("INSERT INTO tb_temp_rts (id_temp, stock_rts, stock_rtu, stock_perbaikan, stock_sisa, stock_cup_sehat, id_jns_brg)
					VALUES(NULL, '0', '0', '0', '0', '0', '$currentJns[id_jns_brg]')");

		if($insert)
			sukses('Data berhasil disimpan', '?pages=jenis_barang');
		else
			sukses('Data gagal disimpan', '?pages=jenis_barang');
	}
	
}elseif ($act=='del') {
	# code...
	$id_jns_brg = (int) filter($_GET['id']);

	$delete = mysql_query("DELETE FROM tb_jns_barang WHERE id_jns_brg=$id_jns_brg");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=kode');
}
?>					